package com.webchat.common.helper;

import com.webchat.common.constants.CookieConstants;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @Author 程序员七七 ====> https://www.coderutil.com 网站作者
 */
public class SessionHelper {

    /**
     * thread local实体类
     */
    private static ThreadLocal<ConcurrentHashMap<String, Object>> threadLocalHolder = new ThreadLocal<>();

    /**
     * IP
     */
    private static final String SESSION_KEY_IP = "CURRENT-IP";

    /**
     * 请求UA信息KEY
     */
    private static final String SESSION_KEY_USER_AGENT = "User-Agent";

    /**
     * TraceId
     */
    private static final String SESSION_KEY_TRACE_ID = "TRACE-ID";

    private static final String SESSION_KEY_TOKEN = "TOKEN";

    private static final String SESSION_KEY_LOG_ID = "LOG-ID";

    private static final String SESSION_KEY_TASK_ID = "TASK-ID";

    public static final String CURRENT_USER_ID = "CURRENT_USER_ID";

    public static final String CONTEXT_USER_ID = "CONTEXT_USER_ID";

    public static final String CURRENT_APP_ID = "CONTEXT_APP_ID";

    public static final String SENDER_TYPE = "SENDER_TYPE";

    public static final String RECEIVER_TYPE = "RECEIVER_TYPE";


    public static void setClientUserInfo(String userId, String ip, String taskId) {
        setUserId(userId);
        setClientIP(ip);
        setTaskId(taskId);
    }

    /**
     * 设置登录用户，谨慎使用！会覆盖原来的userId，一般在拦截器里使用。
     */
    public static void setUserId(String userId) {
        if (StringUtils.isNotBlank(userId)) {
            setAttribute(CURRENT_USER_ID, userId);
        }
    }

    /***
     * 设置客户端IP
     * @param ip
     */
    public static void setClientIP(String ip) {
        if (StringUtils.isNotBlank(ip)) {
            setAttribute(SESSION_KEY_IP, ip);
        }
    }

    /***
     * 设置TraceId
     * @param traceId
     */
    public static void setTraceId(String traceId) {
        if (StringUtils.isNotBlank(traceId)) {
            setAttribute(SESSION_KEY_TRACE_ID, traceId);
        }
    }

    /**
     * 设置logId
     * @param logId
     */
    public static void setLogId(String logId) {
        if (StringUtils.isNotBlank(logId)) {
            setAttribute(SESSION_KEY_LOG_ID, logId);
        }
    }
    /**
     * 设置Token
     * @param token
     */
    public static void setToken(String token) {
        if (StringUtils.isNotBlank(token)) {
            setAttribute(SESSION_KEY_TOKEN, token);
        }
    }

    public static void setAppId(String appId) {
        if (StringUtils.isNotBlank(appId)) {
            setAttribute(CURRENT_APP_ID, appId);
        }
    }

    public static void setContextUserId(String contextUserId) {
        if (StringUtils.isNotBlank(contextUserId)) {
            setAttribute(CONTEXT_USER_ID, contextUserId);
        }
    }

    /***
     * 设置任务ID
     * @param taskId
     */
    public static void setTaskId(String taskId) {
        if (StringUtils.isNotBlank(taskId)) {
            setAttribute(SESSION_KEY_TASK_ID, taskId);
        }
    }

    /**
     * 获取当前登录USER ID
     * @return
     */
    public static String getCurrentUserId() {

        return safeGetAttribute(CURRENT_USER_ID);
    }

    public static Integer getSenderAccountType() {

        String typeValue = safeGetAttribute(SENDER_TYPE);
        return typeValue == null ? null : Integer.valueOf(typeValue);
    }

    public static Integer getReceiverAccountType() {

        String typeValue = safeGetAttribute(RECEIVER_TYPE);
        return typeValue == null ? null : Integer.valueOf(typeValue);
    }

    public static String getTranceId() {

        return safeGetAttribute(SESSION_KEY_TRACE_ID);
    }

    public static String getLogId() {

        return safeGetAttribute(SESSION_KEY_LOG_ID);
    }

    public static String getCurrentAppId() {

        return safeGetAttribute(CURRENT_APP_ID);
    }

    public static String getToken() {

        return safeGetAttribute(SESSION_KEY_TOKEN);
    }

    /**
     * 获取当前主现成副本中的上下文临时用户Id
     * @return
     */
    public static String getContextUserId() {

        return safeGetAttribute(CONTEXT_USER_ID);
    }

    /***
     * 获取客户端IP
     * @return
     */
    public static String getCurrentClientIP() {

        return safeGetAttribute(SESSION_KEY_IP);
    }

    /***
     * 获取客户端UA信息
     * @return
     */
    public static String getCurrentUserAgent() {

        return safeGetAttribute(SESSION_KEY_USER_AGENT);
    }

    /**
     * 线程副本中安全获取信息
     *
     * @return
     */
    public static String safeGetAttribute(String key) {
        Object valueObj = getAttribute(key);
        return valueObj == null ? null : valueObj.toString();
    }

    /**
     * 清除本线程保存的数据,不要设置为null，会有内存泄漏
     */
    public static void clear() {
        threadLocalHolder.remove();
    }

    /**
     * 获取本地的session,只是一个线程的操作，不会出现多线程问题。
     * @return
     */
    private static Map<String, Object> getLocalSession() {
        if (threadLocalHolder.get() == null) {
            threadLocalHolder.set(new ConcurrentHashMap<>());
        }
        return threadLocalHolder.get();
    }

    private static void setAttribute(String key, Object value) {
        getLocalSession().put(key, value);
    }

    private static Object getAttribute(String key) {
        return getLocalSession().get(key);
    }
}
